package com.service.impl;

import com.Dao.userDao;
import com.pojo.User;
import com.service.UserService;
import com.utils.IdUtils;
import com.utils.UserException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class userServiceImpl implements UserService {
    @Autowired
    private userDao userDao;

    public User login(String username, String password) throws UserException {
        User user = userDao.login(username, password);
        if (userDao.findUserByUsername(username)) {
            if (user == null) {
                throw new UserException(10001, "密码错误");
            }
        } else {
            throw new UserException(10001, "用户名不存在");
        }
        return user;
    }

    @Override
    public User findUserById(Integer id) throws UserException {
        User user = userDao.findUserById(id);
        if (user == null) {
            throw new UserException(10001, "用户不存在");
        }
        return user;
    }

    @Override
    public Boolean addUser(User user) throws Exception {
        user.setId(IdUtils.autoOrderId());
        if (userDao.addUser(user)) {
            return true;
        } else {throw new UserException(10003,"用户添加失败");}
    }

    @Override
    public Boolean updateUser(User user) throws UserException {
        System.out.println(user);
        System.out.println(userDao.updateUser(user));
        if(userDao.updateUser(user)){
            return true;
        }else {throw new UserException(10004,"用户修改失败");}
    }

    @Override
    public Boolean deleteUser(Integer id) throws UserException {
        if(userDao.deleteUser(id)){
            return true;
        }else{throw new UserException(10005,"删除失败");}
    }

    @Override
    public List<User> findAllUser() throws UserException {
        List<User> userList=userDao.findAllUser();
        if(userList!=null){
            return userList;
        }else throw new UserException(10006,"查找失败");
    }
}
